package com.wl.cloud.system.controller;

import com.wl.cloud.core.dto.DataStoreDTO;
import com.wl.cloud.core.dto.RestResultDTO;
import com.wl.cloud.system.service.SysRoleService;
import com.wl.cloud.system.support.dto.RoleRightsDTO;
import com.wl.cloud.system.support.dto.SysRoleDTO;
import com.wl.cloud.system.support.dto.query.SysRoleQueryDTO;
import com.wl.cloud.system.support.utils.ConstantSystem;
import com.wl.cloud.system.support.vo.SysRoleVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 角色表 前端控制器
 *
 * @author wanglin
 * @since 2023-09-18
 */
@Tag(name = "角色表", description = "SysRoleController")
@Validated
@CrossOrigin
@RestController
@RequestMapping(ConstantSystem.PROJECT_URL_PREFIX + "sys/role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 分页查询
     *
     * @param pageable
     * @return
     */
    @Operation(summary = "分页")
    @RequestMapping(value = "/page", method = {RequestMethod.POST, RequestMethod.GET})
    public RestResultDTO<DataStoreDTO<SysRoleVO>> page(@ParameterObject @PageableDefault(sort = "orderIndex", direction = Sort.Direction.ASC) Pageable pageable,
                                                       @ParameterObject SysRoleQueryDTO queryDto) {
        return RestResultDTO.newSuccess(sysRoleService.page(pageable, queryDto));
    }

    /**
     * 列表查询
     *
     * @param sort
     * @return
     */
    @Operation(summary = "列表")
    @RequestMapping(value = "/list", method = {RequestMethod.POST, RequestMethod.GET})
    public RestResultDTO<List<SysRoleVO>> list(@ParameterObject @SortDefault(sort = "createTime", direction = Sort.Direction.DESC) Sort sort,
                                               @ParameterObject SysRoleQueryDTO queryDto) {
        return RestResultDTO.newSuccess(sysRoleService.list(sort, queryDto));
    }

    /**
     * 保存
     *
     * @param dto
     * @return
     */
    @Operation(summary = "保存")
    @PostMapping("/save")
    public RestResultDTO<Void> save(@Validated @RequestBody SysRoleDTO dto) {
        sysRoleService.save(dto);
        return RestResultDTO.newSuccess();
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Operation(summary = "修改")
    @PostMapping(value = "/update")
    public RestResultDTO<Void> update(@Validated @RequestBody SysRoleDTO dto) {
        sysRoleService.update(dto);
        return RestResultDTO.newSuccess();
    }

    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    @Operation(summary = "查询详情")
    @GetMapping("/get")
    public RestResultDTO<SysRoleVO> get(@Parameter(description = "记录ID") @RequestParam String id) {
        return RestResultDTO.newSuccess(sysRoleService.get(id));
    }

    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    @Operation(summary = "查询详情")
    @GetMapping("/getDetail")
    public RestResultDTO<SysRoleVO> getDetail(@Parameter(description = "记录ID") @RequestParam String id) {
        return RestResultDTO.newSuccess(sysRoleService.getDetail(id));
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/delete", method = {RequestMethod.GET})
    public RestResultDTO<Void> delete(@RequestParam Set<String> ids) {
        sysRoleService.delete(ids);
        return RestResultDTO.newSuccess(null, "删除成功");
    }

    @Operation(summary = "分配角色权限")
    @RequestMapping(value = "distributeRights", method = {RequestMethod.POST})
    public RestResultDTO<Void> distributeRights(@Parameter(description = "分配角色入参") @Validated @RequestBody RoleRightsDTO dto) {
        sysRoleService.distributeRights(dto);
        return RestResultDTO.newSuccess(null, "操作成功");
    }

    /**
     * 根据用户账号获取授权角色,过滤本身已有的角色
     */
    @Operation(summary = "根据用户账号获取授权角色")
    @RequestMapping(value = "selectRolesByUserId", method = {RequestMethod.GET})
    public RestResultDTO<DataStoreDTO<SysRoleVO>> selectRolesByUserId(@ParameterObject @PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable,
                                                                      @Parameter(description = "用户账号ID") @RequestParam String userId) {
        return RestResultDTO.newSuccess(sysRoleService.selectRolesByUserId(pageable, userId));
    }

    @Operation(summary = "启动或禁用用户userId——管理员")
    @RequestMapping(value = "updateStatusById", method = {RequestMethod.GET})
    public RestResultDTO<Void> updateStatusById(@Parameter(description = "角色Id") @RequestParam String roleId) {
        sysRoleService.updateStatusById(roleId);
        return RestResultDTO.newSuccess();
    }
}
